from django.shortcuts import render,redirect
from .models import KucunPost,ZhizuoPost,LingquPost,ZhizuoNote
from django.http import HttpResponse
from .forms import KucunPostForm
from kucun import models
# Create your views here.

#主页展示页面
def kucun_list(request):
    kucun = KucunPost.objects.get(id=1)
    zhizuo = ZhizuoPost.objects.all()
    lingqu = LingquPost.objects.all()

    context = {
        'kucun':kucun,
        'zhizuo':zhizuo,
        'lingqu':lingqu,
    }
    return render(request,'kucun/list.html',context)


#库存添加
def kucun_add(request):
    if request.method == "POST":
        new_kucun = request.POST
        
        kucun = KucunPost.objects.get(id=1)       
        if new_kucun:
            kucun.ban_num = int(new_kucun['ban_num']) + kucun.ban_num
            kucun.green_num = int(new_kucun['green_num']) + kucun.green_num
            kucun.red_num = int(new_kucun['red_num']) + kucun.red_num
            kucun.yellow_num = int(new_kucun['yellow_num']) + kucun.yellow_num
            kucun.dianzu_num = int(new_kucun['dianzu_num']) + kucun.dianzu_num
            kucun.erjiguan_num = int(new_kucun['erjiguan_num']) + kucun.erjiguan_num
            kucun = kucun.save()           
            return redirect("kucun:kucun_list")
        else:
            return HttpResponse("表单内容有误，请重新填写。")
    else:
        # kucun_post_form = KucunPostForm()
        # context = {'kucun_post_form':kucun_post_form }
        return render(request,'kucun/kucun_add.html')

#制作灯板的数量增加
def zhizuo_add(request):
    if request.method == "POST":
        new_zhizuo = request.POST
        zhizuo = ZhizuoPost.objects.get(zhizuo_name=new_zhizuo['zhizuo_name'])       
        if new_zhizuo:
            zhizuo.zhizuo_date = new_zhizuo.get('date')
            zhizuo.han_green_num = int(new_zhizuo['han_green_num']) + zhizuo.han_green_num
            zhizuo.han_yellow_num = int(new_zhizuo['han_yellow_num']) + zhizuo.han_yellow_num
            zhizuo.han_red_num = int(new_zhizuo['han_red_num']) + zhizuo.han_red_num
            #计算总数
            zhizuo.han_total_num = zhizuo.han_green_num + zhizuo.han_yellow_num + zhizuo.han_red_num           
            zhizuo.cha_num = int(new_zhizuo['cha_num']) + zhizuo.cha_num
            zhizuo.jian_num = int(new_zhizuo['jian_num']) + zhizuo.jian_num
            zhizuo = zhizuo.save()
            #每插一个颜色的板，减少库存中的灯数量450
            kucun = KucunPost.objects.get(id=1)
            kucun.green_num = kucun.green_num - int(new_zhizuo['han_green_num']) * 450
            kucun.yellow_num = kucun.yellow_num - int(new_zhizuo['han_yellow_num']) * 450
            kucun.red_num = kucun.red_num - int(new_zhizuo['han_red_num']) * 450
            #设置变量，每一次增加的三种颜色的灯板总数
            new_zhizuo_han = int(new_zhizuo['han_green_num']) + int(new_zhizuo['han_yellow_num']) + int(new_zhizuo['han_red_num'])
            #插一个板，减少电阻和二极管，空板
            kucun.dianzu_num = kucun.dianzu_num - new_zhizuo_han * 100
            kucun.erjiguan_num = kucun.erjiguan_num - new_zhizuo_han * 100
            kucun.ban_num = kucun.ban_num - new_zhizuo_han
            #成品数量增加
            kucun.chengpin_num = kucun.chengpin_num + new_zhizuo_han
            kucun = kucun.save()
            #增加制作记录
            models.ZhizuoNote.objects.create(
                new_name = new_zhizuo.get('zhizuo_name'),
                new_date = new_zhizuo.get('date'),
                new_green_num = new_zhizuo.get('han_green_num'),
                new_yellow_num = new_zhizuo.get('han_yellow_num'),
                new_red_num = new_zhizuo.get('han_red_num'),
                new_total_num = new_zhizuo_han,
                new_cha_num = new_zhizuo.get('cha_num'),
                new_jian_num = new_zhizuo.get('jian_num'),
            )
            #增加记录
            return redirect("kucun:kucun_list")
        else:
            return HttpResponse("表单内容有误，请重新填写。")
    else:
        # kucun_post_form = KucunPostForm()
        # context = {'kucun_post_form':kucun_post_form }
        return render(request,'kucun/zhizuo_add.html')

#制作的记录

def zhizuo_note(request):
    note = ZhizuoNote.objects.all()

    context = {
        'note':note,
    }
    return render(request,'kucun/zhizuo_note.html',context)